﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.IO;
    using System.Web.UI.Design;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    internal class GridSpecialFolderEditor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService;
        private FolderBrowserDialog folderBrowser;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }
            if (this.editorService != null)
            {
                string specialFolderName = "special folder";
                if (context.PropertyDescriptor.Name == "FolderStyle")
                {
                    specialFolderName = "folder with styles";
                }
                else if (context.PropertyDescriptor.Name == "FolderLocalization")
                {
                    specialFolderName = "folder with localization XML files";
                }
                else if (context.PropertyDescriptor.Name == "FolderExports")
                {
                    specialFolderName = "folder for exporting";
                }
                this.folderBrowser = new FolderBrowserDialog();
                this.folderBrowser.ShowNewFolderButton = false;
                this.folderBrowser.Description = "Please select the " + specialFolderName + ".\r\nThe " + specialFolderName + " must be located within the project folder.";
                string fullPath = "";
                try
                {
                    IWebFormsDocumentService service = provider.GetService(typeof(IWebFormsDocumentService)) as IWebFormsDocumentService;
                    fullPath = Path.GetFullPath(service.DocumentUrl.Replace("file:///", ""));
                    IProjectItem projectItemFromUrl = ((IWebApplication) provider.GetService(typeof(IWebApplication))).GetProjectItemFromUrl(value.ToString());
                    if (projectItemFromUrl != null)
                    {
                        DirectoryInfo info = new DirectoryInfo(projectItemFromUrl.PhysicalPath);
                        if (info.Exists)
                        {
                            this.folderBrowser.SelectedPath = projectItemFromUrl.PhysicalPath;
                        }
                        else
                        {
                            this.folderBrowser.SelectedPath = fullPath;
                        }
                    }
                    else
                    {
                        this.folderBrowser.SelectedPath = fullPath;
                    }
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                }
                if (this.folderBrowser.ShowDialog() != DialogResult.OK)
                {
                    return value;
                }
                string selectedPath = this.folderBrowser.SelectedPath;
                if (!string.IsNullOrEmpty(fullPath))
                {
                    value = GridIOHelper.EvaluateRelativePath(fullPath, selectedPath, specialFolderName);
                    return value;
                }
                value = selectedPath;
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}

